﻿package export.strength
{
	import base.BaseHero;
	
	import com.dusk.util.TextUtil;
	import com.game.data.constant.AttributeName;
	import com.game.data.vo.EquipmentVO;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	import com.game.view.component.notification.TipBox;
	
	import config.Config;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class SutraInterface extends Sprite
	{
		
		public function SutraInterface()
		{
			super();
			TextUtil.redrawTxtOld(this, needRedrawText);
			gc = Config.getIns();
			addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
			addEventListener(Event.REMOVED_FROM_STAGE, removed, false, 0, true);
		}
		
		public var resetbtn:SimpleButton;
		public var txt_fbname:TextField;
		public var txt_fbdj:TextField;
		public var txt_fbczl:TextField;
		public var txt_fbwx:TextField;
		public var txt_fbatk:TextField;
		public var txt_fbdef:TextField;
		public var txt_fbhx:TextField;
		public var txt_fbhl:TextField;
		public var txt_fblh:TextField;
		public var btn_sj:SimpleButton;
		
		public var btn_close:SimpleButton;
		
		public var lhmc:MovieClip;
		
		public var showmc:MovieClip;
		
		public var introducemc:MovieClip;
		private var needRedrawText:Array = ["txt_fbname", "txt_fbdj", "txt_fbczl", "txt_fbwx", "txt_fbatk", "txt_fbdef", "txt_fbhx", "txt_fbhl", "txt_fblh"];
		private var currentSura:EquipmentVO;
		private var oldEquip:EquipmentVO;
		private var hero:BaseHero;
		private var gc:Config;
		
		public function setRole(param1:BaseHero):void
		{
			hero = param1;
			currentSura = hero.getPlayer().getCurEquipByType("zbfb");
			if (currentSura)
			{
				oldEquip = currentSura;
				showmc.gotoAndStop(currentSura.name);
				introducemc.gotoAndStop(currentSura.name);
				setTxt();
			}
		}
		
		private function setTxt():void
		{
			if (currentSura)
			{
				var percentage:int = int(hero.getPlayer().lhValue / (getNextGradeLHValue(currentSura.getLevel()) + 1) * 50);
				lhmc.gotoAndStop(percentage > 100 ? 100 : percentage);
				txt_fblh.text = int(hero.getPlayer().lhValue) + "/" + getNextGradeLHValue(currentSura.getLevel());
				txt_fbname.text = currentSura.name;
				txt_fbdj.text = currentSura.getLevel() + "";
				txt_fbczl.text = currentSura.getGrowthRate() + "";
				txt_fbwx.text = currentSura.getFiveElementString();
				txt_fbatk.text = currentSura.getAttributeValue(AttributeName.ATK).toString();
				txt_fbdef.text = currentSura.getAttributeValue(AttributeName.DEF).toString();
				txt_fbhx.text = currentSura.getAttributeValue(AttributeName.HP_HEAL).toString();
				txt_fbhl.text = currentSura.getAttributeValue(AttributeName.MP_HEAL).toString();
			}
		}
		
		private function upDataZSTimer():void
		{
			var box:TipBox = new TipBox("", updateByZSXP);
			var data:Object = getNextGradeZSJLValue(currentSura.getLevel() + 1);
			box.setText("您确定将法宝升级到" + (currentSura.getLevel() + 1) + "级，消耗" + data.num + "个" + data.cname + "?");
			box.add();
		}
		
		private function upDataGod():void
		{
			var _loc1_:TipBox = new TipBox("", updateByJade);
			var _loc2_:Object = getNextGradeKLYValue(currentSura.getLevel() + 1);
			_loc1_.setText("您确定将法宝升级到" + (currentSura.getLevel() + 1) + "级，消耗" + _loc2_.num + "个" + _loc2_.cname + "?");
			_loc1_.add();
		}
		
		private function getNextGradeZSJLValue(param1:int):Object
		{
			var _loc2_:* = {};
			if (param1 <= 5)
			{
				_loc2_.kly = "zsTimerup1";
				_loc2_.num = param1 - 1;
				_loc2_.cname = "烛时星魄1";
			}
			else if (param1 > 5 && param1 <= 10)
			{
				_loc2_.kly = "zsTimerup2";
				_loc2_.num = Math.ceil((param1 - 5) * 1.5);
				_loc2_.cname = "烛时星魄2";
			}
			return _loc2_;
		}
		
		private function getNextGradeKLYValue(param1:int):Object
		{
			var _loc2_:* = {};
			var _loc3_:uint = currentSura.equipId == "tjbg" ? 1 : 0;
			if (param1 <= 5)
			{
				_loc2_.kly = "kly4";
				_loc2_.num = param1 - 1 + _loc3_;
				_loc2_.cname = "4级昆仑玉";
			}
			else if (param1 > 5)
			{
				_loc2_.kly = "kly5";
				_loc2_.num = Math.ceil((param1 - 5 + _loc3_) * 1.5);
				_loc2_.cname = "5级昆仑玉";
			}
			return _loc2_;
		}
		
		private function getNextGradeLHValue(param1:int):int
		{
			return param1 * param1 * 1000;
		}
		
		private function getNextGradeZZValue(param1:int):int
		{
			return param1 * param1 * 3;
		}
		
		private function added(param1:Event):void
		{
			btn_close.addEventListener(MouseEvent.CLICK, close);
			resetbtn.addEventListener(MouseEvent.CLICK, refreshWX);
			btn_sj.addEventListener(MouseEvent.CLICK, sjMethod);
			EventManager.getIns().dispatchEvent(GameEventType.PAUSE_GAME);
		}
		
		private function removed(param1:Event):void
		{
			btn_close.removeEventListener(MouseEvent.CLICK, close);
			resetbtn.removeEventListener(MouseEvent.CLICK, refreshWX);
			btn_sj.removeEventListener(MouseEvent.CLICK, sjMethod);
			hero = null;
		}
		
		private function close(param1:Event):void
		{
			hero.roleProperties.removeEquip(oldEquip, false);
			hero.roleProperties.addEquip(currentSura);
//			hero.getPlayer().setCurEquipByType("zbfb", currentSura);
			EventManager.getIns().dispatchEvent(GameEventType.CONTINUE_GAME);
			if (parent)
			{
				parent.removeChild(this);
			}
		}
		
		private function sjMethod(param1:Event):void
		{
			var _loc4_:* = undefined;
			var _loc5_:* = undefined;
			var _loc2_:int = 0;
			var _loc3_:* = null;
			if (currentSura)
			{
				if (currentSura.equipId == "zsTimer")
				{
					if (currentSura.getLevel() < 10)
					{
						upDataZSTimer();
						return;
					}
					NotificationManager.alert("已达最高等级");
					return;
				}
				if (currentSura.equipId == "mdhf")
				{
					if (currentSura.getLevel() < 10)
					{
						upDataGod();
						return;
					}
					NotificationManager.alert("已达最高等级");
					return;
				}
				if (currentSura.equipId == "jyhl")
				{
					if (currentSura.getLevel() < 10)
					{
						upDataGod();
						return;
					}
					NotificationManager.alert("已达最高等级");
					return;
				}
				if (currentSura.equipId == "tjbg")
				{
					if (currentSura.getLevel() < 9)
					{
						upDataGod();
						return;
					}
					NotificationManager.alert("已达最高等级");
					return;
				}
				if (currentSura.equipId == "stlp")
				{
					if (currentSura.getLevel() >= 10)
					{
						NotificationManager.alert("已达最高等级");
						return;
					}
				}
				if (currentSura.equipId == "xhmt")
				{
					if (currentSura.getLevel() >= 10)
					{
						NotificationManager.alert("已达最高等级");
						return;
					}
				}
				_loc2_ = getNextGradeLHValue(currentSura.getLevel());
				if (hero.getPlayer().lhValue >= _loc2_)
				{
					if (currentSura.getLevel() < 10)
					{
						hero.roleProperties.removeEquip(currentSura, false);
						_loc3_ = currentSura.getGrowthDataByName(currentSura.equipId);
						currentSura.growth(_loc3_, true);
						hero.getPlayer().reduceLhValue(_loc2_);
						setTxt();
						hero.roleProperties.addEquip(currentSura);
					}
					else if (currentSura.getLevel() >= 10 && currentSura.getLevel() < 15)
					{
						_loc4_ = getNextGradeZZValue(currentSura.getLevel() - 9);
						_loc5_ = new TipBox("", upDateByTears);
						_loc5_.setText("您确定将法宝升级到" + (currentSura.getLevel() + 1) + "级，消耗" + _loc4_ + "龙女的眼泪?");
						_loc5_.add();
					}
					else
					{
						NotificationManager.alert("已达最高等级");
					}
				}
			}
		}
		
		private function updateByZSXP(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			var _loc3_:Object = getNextGradeZSJLValue(currentSura.getLevel() + 1);
			if (hero.getPlayer().getStuffNumByName(_loc3_.kly) >= _loc3_.num)
			{
				hero.roleProperties.removeEquip(currentSura);
				_loc2_ = currentSura.getGrowthDataByName(currentSura.equipId);
				currentSura.growth(_loc2_);
				hero.getPlayer().removeEquipFormBag(_loc3_.kly, 2, _loc3_.num);
				setTxt();
				hero.roleProperties.addEquip(currentSura);
			}
			else
			{
				NotificationManager.alert(_loc3_.cname + "数量不够");
			}
			if (introducemc["saymc"])
			{
				if (currentSura.getLevel() >= 6)
				{
					introducemc["saymc"].gotoAndStop(2);
				}
			}
		}
		
		private function updateByJade(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			var _loc3_:Object = getNextGradeKLYValue(currentSura.getLevel() + 1);
			if (hero.getPlayer().getStuffNumByName(_loc3_.kly) >= _loc3_.num)
			{
				hero.roleProperties.removeEquip(currentSura);
				_loc2_ = currentSura.getGrowthDataByName(currentSura.equipId);
				currentSura.growth(_loc2_);
				hero.getPlayer().removeEquipFormBag(_loc3_.kly, 2, _loc3_.num);
				setTxt();
				hero.roleProperties.addEquip(currentSura);
			}
			else
			{
				NotificationManager.alert(_loc3_.cname + "数量不够");
			}
			if (introducemc["saymc"])
			{
				if (currentSura.getLevel() >= 6)
				{
					introducemc["saymc"].gotoAndStop(2);
				}
			}
		}
		
		private function upDateByTears(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			var _loc3_:int = getNextGradeZZValue(currentSura.getLevel() - 9);
			if (hero.getPlayer().getStuffNumByName("wplvdyl") >= _loc3_)
			{
				hero.roleProperties.removeEquip(currentSura);
				_loc2_ = currentSura.getGrowthDataByName(currentSura.equipId);
				currentSura.growth(_loc2_);
				hero.getPlayer().removeEquipFormBag("wplvdyl", 2, _loc3_);
				setTxt();
				hero.roleProperties.addEquip(currentSura);
			}
			else
			{
				NotificationManager.alert("龙女的眼泪不够");
			}
		}
		
		private function refreshWX(param1:MouseEvent):void
		{
			var _loc1_:TipBox = new TipBox("", refreshConfirm);
			var _loc2_:int = hero.getPlayer().getStuffNumByName("wpccfq");
			_loc1_.setText("您确定使用3个传承法器重置法宝五行？当前有" + _loc2_ + "个");
			_loc1_.add();
		}
		
		private function refreshConfirm(param1:MouseEvent):void
		{
			var _loc5_:int = 0;
			var _loc2_:int = hero.getPlayer().getStuffNumByName("wpccfq");
			var _loc3_:* = null;
			var _loc4_:int = 1;
			if (currentSura)
			{
				if (_loc2_ < 3)
				{
					NotificationManager.alert("传承法器数量不够!");
					return;
				}
				_loc5_ = currentSura.getLevel();
				hero.roleProperties.removeEquip(currentSura, false);
				currentSura = gc.allEquip.findByName(currentSura.equipId);
				_loc3_ = currentSura.getGrowthDataByName(currentSura.equipId);
				while (_loc4_++ < _loc5_)
				{
					currentSura.growth(_loc3_, true);
				}
				setTxt();
				hero.roleProperties.addEquip(currentSura);
				hero.getPlayer().removeEquipFormBag("wpccfq", 2, 3);
			}
			else
			{
				NotificationManager.alert("当前法宝异常！");
			}
		}
	}
}
